/**
 * @copyright 2017 sinping.com 北京芯平科技股份有限公司. All rights reserved.
 * 本内容仅限于北京芯平科技股份有限公司内部传阅，禁止外泄以及用于其他的商业目的.
 */
package com.sinping.qh.api.auth;

import com.sinping.qh.api.support.FrontPage;
import com.sinping.qh.api.support.ResDto;
import com.sinping.qh.utils.mapper.JsonMapper;
import com.sinping.qh.api.support.Const;
import com.sinping.qh.api.support.ConstEum;
import com.sinping.qh.dto.auth.OnlineUserDTO;
import com.sinping.qh.service.auth.IResourceService;
import com.sinping.qh.service.auth.ISessionService;
import com.sinping.qh.service.auth.IUserService;
import com.sinping.qh.utils.text.MoreStringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 在线用户管理
 *
 * @author xuerunjia
 * @create 2017/12/21
 */
@Api(value = "Admin在线用户管理", description = "Admin在线用户管理API")
@RestController
@RequestMapping(name = "在线用户管理", value = Const.BASE_URI + "online")
public class OnlineUserPoint {

    private final static Logger log = LoggerFactory.getLogger(OnlineUserPoint.class);

    @Autowired
    private IUserService userService;
    @Autowired
    private IResourceService resourceService;

    @Autowired
    @SuppressWarnings("SpringJavaAutowiringInspection")
    private ISessionService sessionService;

    @ApiParam(name = "", value = "无")
    @ApiOperation(value = "在线用户列表")
    @RequestMapping(name = "在线用户列表", method = RequestMethod.GET)
    public ResDto list() {
        log.info("进入在线用户列表");
        try {
            List<OnlineUserDTO> onlineUserDTOList = this.sessionService.selectOnlineUser(); //获得在线用户列表
            FrontPage<OnlineUserDTO> frontPage = new FrontPage<OnlineUserDTO>();
            frontPage.setTotal(onlineUserDTOList.size());
            frontPage.setRecords(onlineUserDTOList);

            String jsonData = JsonMapper.defaultMapper().toJson(frontPage);
            log.info("结束在线用户列表，结果为：{}", jsonData);
            ResDto resDto = new ResDto(ConstEum.SUCCESS);
            resDto.setDatas(jsonData);
            return resDto;
        } catch (Exception e) {
            log.error("在线用户列表加载失败，错误为：{}", e.getMessage());
            return new ResDto(ConstEum.SERVER_ERROR);
        }
    }


    @ApiOperation(value = "踢出用户")
    @RequestMapping(name = "踢出用户", method = RequestMethod.PUT)
    public ResDto logOutUser(@ApiParam(name = "msg", value = "要踢出用户的主键") @RequestParam(required = true) String msg) {
        log.info("进入踢出用户方法，参数为：{} 。", msg);
        ResDto result = new ResDto(ConstEum.PARAM_ERROR);
      
        OnlineUserDTO onlineUserDTO = JsonMapper.defaultMapper().fromJson(msg, OnlineUserDTO.class);
        if (onlineUserDTO == null || MoreStringUtil.isNull(onlineUserDTO.getId())) {
            result.setMsg("msg.param.format.error");
            return result;
        }
        try {
            Long is_success = sessionService.deleteSessionAndTokenByUserid(onlineUserDTO.getId());
            if (is_success > 0) {
                return new ResDto(ConstEum.SUCCESS);
            }
            log.error("踢出用户失败，服务层返回值：{}", is_success);
            result.setMsg("msg.handle.fail");
            return result;
        } catch (Exception e) {
            log.error("踢出用户异常", e);
            return new ResDto(ConstEum.SERVER_ERROR);
        }
    }

}
